/*=============================================================================
    test/test-geom.js
    Copyright (C) 2008-2009, Donald W. Griffin
    All rights reserved.
    [MIT license :: see license.txt for details]
=============================================================================*/

gTests.push
(
    // Point
    function ()
    {
        var pt = $point();
        assertEquals(0, pt.x);
        assertEquals(0, pt.y);
    },
    function ()
    {
        var pt = $point(5);
        assertEquals(5, pt.x);
        assertEquals(5, pt.y);
    },
    function ()
    {
        var pt = $point(7, 9);
        assertEquals(7, pt.x);
        assertEquals(9, pt.y);
    },
    function ()
    {
        var pt = $point(7, 9);
        assertEquals(7, pt.X());
        assertEquals(9, pt.Y());
    },
    function ()
    {
        var pt = $point();
        pt.offset(10, -5);
        assertEquals(10, pt.x);
        assertEquals(-5, pt.y);
    },
    function ()
    {
        var pt = $point(7, 9);
        pt.offset(10);
        assertEquals(17, pt.x);
        assertEquals(19, pt.y);
    },
    function ()
    {
        var pt = $point(7, 9);
        pt.offset(10, 20);
        assertEquals(17, pt.x);
        assertEquals(29, pt.y);
    },
    function ()
    {
        var pt = $point(7, 9);
        pt.offset(-10, -20);
        assertEquals(-3, pt.x);
        assertEquals(-11, pt.y);
    },

    // Size
    function ()
    {
        var sz = $size();
        assertEquals(0, sz.w);
        assertEquals(0, sz.h);
    },
    function ()
    {
        var sz = $size(5);
        assertEquals(5, sz.w);
        assertEquals(5, sz.h);
    },
    function ()
    {
        var sz = $size(7, 9);
        assertEquals(7, sz.w);
        assertEquals(9, sz.h);
    },
    function ()
    {
        var sz = $size(7, 9);
        assertEquals(7, sz.W());
        assertEquals(9, sz.H());
    },
    function ()
    {
        var sz = $size();
        sz.grow(10, -5);
        assertEquals(10, sz.w);
        assertEquals(-5, sz.h);
    },
    function ()
    {
        var sz = $size(7, 9);
        sz.grow(10);
        assertEquals(17, sz.w);
        assertEquals(19, sz.h);
    },
    function ()
    {
        var sz = $size(7, 9);
        sz.grow(10, 20);
        assertEquals(17, sz.w);
        assertEquals(29, sz.h);
    },
    function ()
    {
        var sz = $size(7, 9);
        sz.grow(-10, -20);
        assertEquals(-3, sz.w);
        assertEquals(-11, sz.h);
    },

    // Span
    function ()
    {
        var sp = new jibits.Span();
        assertEquals(0, sp.beg);
        assertEquals(0, sp.end);
        assertEquals(0, sp.size());
    },
    function ()
    {
        var sp = new jibits.Span(5);
        assertEquals(5, sp.beg);
        assertEquals(5, sp.end);
        assertEquals(0, sp.size());
    },
    function ()
    {
        var sp = new jibits.Span(7, 9);
        assertEquals(7, sp.beg);
        assertEquals(9, sp.end);
        assertEquals(2, sp.size());
    },
    function ()
    {
        var sp = new jibits.Span(7, 9);
        sp.offset(10);
        assertEquals(17, sp.beg);
        assertEquals(19, sp.end);
        assertEquals(2, sp.size());
    },
    function ()
    {
        var sp = new jibits.Span(7, 9);
        assertTrue(sp.contains(7));
        assertTrue(sp.contains(8));
        assertFalse(sp.contains(9));
    },

    // Rect
    function ()
    {
        var r = $rect();
        assertEquals(0, r.X());
        assertEquals(0, r.Y());
        assertEquals(0, r.W());
        assertEquals(0, r.H());
        assertEquals(0, r.R());
        assertEquals(0, r.B());
    },
    function ()
    {
        var r = $rect(5, 7, 10, 20);
        assertEquals(5,  r.X());
        assertEquals(7,  r.Y());
        assertEquals(10, r.W());
        assertEquals(20, r.H());
        assertEquals(15, r.R());
        assertEquals(27, r.B());
    },
    function ()
    {
        var r = $rect(5, 7, 10, 20);
        assertEquals(5, r.horz.beg);
        assertEquals(15, r.horz.end);
        assertEquals(7, r.vert.beg);
        assertEquals(27, r.vert.end);
    },
    function ()
    {
        var sz = $rect(5, 7, 10, 20).size();
        assertEquals(10, sz.w);
        assertEquals(20, sz.h);
    },
    function ()
    {
        var r = $rect($point(5, 7), $size(10, 20));
        assertEquals(5,  r.X());
        assertEquals(7,  r.Y());
        assertEquals(10, r.W());
        assertEquals(20, r.H());
        assertEquals(15, r.R());
        assertEquals(27, r.B());
    },
    function ()
    {
        var r = $rect($point(5, 7), $point(15, 27));
        assertEquals(5,  r.X());
        assertEquals(7,  r.Y());
        assertEquals(10, r.W());
        assertEquals(20, r.H());
        assertEquals(15, r.R());
        assertEquals(27, r.B());
    },
    function ()
    {
        var r = $rect(5, 7, 10, 20);
        r.offset(30, 70);
        assertEquals(35, r.X());
        assertEquals(77, r.Y());
        assertEquals(10, r.W());
        assertEquals(20, r.H());
        assertEquals(45, r.R());
        assertEquals(97, r.B());
    },
    function ()
    {
        var r = $rect(5, 7, 10, 20);
        for (var x = 3; x < 20; ++x)
            for (var y = 5; y < 30; ++y)
            {
                var b = (5 <= x && x < 15 && 7 <= y && y < 27);
                assertEquals(b, r.contains(x, y));
                assertEquals(b, r.contains($point(x, y)));
            }
    },
    function ()
    {
        var pt = $rect(5, 7, 10, 20).TL();
        assertEquals(5, pt.x);
        assertEquals(7, pt.y);
    },
    function ()
    {
        var pt = $rect(5, 7, 10, 20).TR();
        assertEquals(15, pt.x);
        assertEquals(7, pt.y);
    },
    function ()
    {
        var pt = $rect(5, 7, 10, 20).BL();
        assertEquals(5, pt.x);
        assertEquals(27, pt.y);
    },
    function ()
    {
        var pt = $rect(5, 7, 10, 20).BR();
        assertEquals(15, pt.x);
        assertEquals(27, pt.y);
    },

    // Placement1D

    function ()
    {
        var lp = new jibits.Placement1D("0%-1");
        var sp = lp.place(new jibits.Span(10, 110), 500);
        assertEquals(501, sp.beg);
    },
    function ()
    {
        var lp = new jibits.Placement1D("100%+1");
        var sp = lp.place(new jibits.Span(10, 210), 500);
        assertEquals(299, sp.beg);
    },
    function ()
    {
        var lp = new jibits.Placement1D("100%");
        var sp = lp.place(new jibits.Span(10, 210), 500);
        assertEquals(300, sp.beg);
    },

    // Placement

    function ()
    {
        var pt = new jibits.Placement("0%,0%").anchor($rect(10, 20, 100, 200));

        assertEquals(10, pt.x);
        assertEquals(20, pt.y);
    },
    function ()
    {
        var pt = new jibits.Placement("50%,100%").anchor($rect(10, 20, 100, 200));

        assertEquals(60, pt.x);
        assertEquals(220, pt.y);
    },
    function ()
    {
        var r = new jibits.Placement("0%,0%").place($rect(10, 20, 100, 200),
                                                    $point(500, 700));
        assertEquals(500, r.X());
        assertEquals(700, r.Y());
        assertEquals(100, r.W());
        assertEquals(200, r.H());
    },
    function ()
    {
        var r = new jibits.Placement("50%,0%").place($rect(10, 20, 100, 200),
                                                     $point(500, 700));
        assertEquals(450, r.X());
        assertEquals(700, r.Y());
        assertEquals(100, r.W());
        assertEquals(200, r.H());
    },

    // Positioning

    function ()
    {
        var pr = jibits.Positioning.TL.place($rect(10, 20, 100, 200),
                                             $point(500, 700), "0%,0%");
        assertEquals(500, pr.left);
        assertEquals(700, pr.top);
    },
    function ()
    {
        var pr = jibits.Positioning.BR.place($rect(10, 20, 100, 200),
                                             $point(500, 700), "0%,0%");
        assertEquals(500, pr.right);
        assertEquals(700, pr.bottom);
    },
    function ()
    {
        var pr = jibits.Positioning.TL.place($rect(10, 20, 100, 200),
                                             $point(500, 700), "50%,0%");
        assertEquals(450, pr.left);
        assertEquals(700, pr.top);
    },

    function () { } // end of list
);
